package comp.movel.ubiqua.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import android.util.Log;

public class SerializerClass {

	public static byte[] serializeObject(Object o) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			ObjectOutput out = new ObjectOutputStream(bos);
			out.writeObject(o);
			out.close();
			// Get the bytes of the serialized object
			byte[] buf = bos.toByteArray();
			return buf;
		} catch (IOException ioe) {
			Log.e("serializeObject", "error", ioe);
			return null;
		}
	}

	public static String serializePlacar(Placar o) {
		String result = o.getPlacarJog1() + ";" + o.getPlacarJog2() + ";"
				+ o.isVezJogador1() + ";" + o.getIdPerguntaCorrente() + ";"
				+ o.getPassaRepassa() + ";" + o.getValorPergunta();
		return result;
	}
	
	 public static Placar deserializePlacar(String readMessage) {
         //String readMessage = new String(readBuf, 0, msg.arg1);
         String[] placarString = readMessage.split(";");
         try{
        	 Placar placar = new Placar(placarString);
        	 return placar;
         }catch (Exception e) {
			return null;
		}
 }

	public static Object deserializeObject(byte[] b) {
		try {
			ObjectInputStream in = new ObjectInputStream(
					new ByteArrayInputStream(b));
			Object object = in.readObject();
			in.close();
			return object;
		} catch (ClassNotFoundException cnfe) {
			Log.e("deserializeObject", "class not found error", cnfe);
			return null;
		} catch (IOException ioe) {
			Log.e("deserializeObject", "io error", ioe);
			return null;
		}
	}

	

}
